<?php

// phpMyRealty 3
//
// File Name: viewuserlistings.php
// File Location : ./
//
// Copyright (c)2009 phpMyRealty.com
//
// e-mail: support@phpMyRealty.com

// Include configuration file and general functions
define('PMR', 'true');

include ( './config.php' );
include ( PATH . '/defaults.php' );

// ----------------------------------------------------------------------
// USER CONTROL PANEL SECTION
// VIEW ALL LISTINGS

// Title tag content
$title = $conf['website_name_short'] . ' - ' . $lang['Edit_Listings'];

// Template header
include ( PATH . '/templates/' . $cookie_template . '/header.php' );

// Authenticate user

// If logged we can start the page output
if (auth_check($session->fetch('login'), $session->fetch('password')))
 {
  // Fetching the user data 
  $sql = 'SELECT * FROM ' . USERS_TABLE . ' WHERE login = "' . $session->fetch('login') . '" LIMIT 1';
  $res = $db->query( $sql );
  $f_res = $db->fetcharray( $res );

  // Order by
  if ($_GET['order_by'] == '')
  {
  	$order_by = 'id';
  	$order_by2 = 'DESC';
  }
  	else
  {
  	$order_by = $_GET['order_by'];
  	$order_by2 = $_GET['order_by2'];
  }
  
	// Title/descr language to use (if available)
	$title = str_replace( 'name', 'title', $language_in );
	$description = str_replace( 'name', 'description', $language_in );
  
  // Fetching the listings data 
  $sql = 'SELECT * FROM ' . PROPERTIES_TABLE . ' WHERE userid = "' . safehtml($f_res['id']) . '"';
  
  // Pagination
  if (isset($_GET['page'])) $page = $_GET['page']; else $page = 0;

  $results_page = $page * $conf['search_results'];

  // Fetching the listings data with the LIMIT function for pagination
  $sql2 = 'SELECT ' . $title . ', ' . $description . ', ' . PROPERTIES_TABLE . '.* FROM ' . PROPERTIES_TABLE  . ' WHERE userid = "' . intval($f_res['id']) . '"  ORDER BY ' . addslashes($order_by) . ' ' . addslashes($order_by2) . ' LIMIT ' . $results_page . ', ' . $conf['search_results'];

  $r = $db->query( $sql ) or error ('Critical Error', mysql_error () );
  $r2 = $db->query( $sql2 ) or error ('Critical Error', mysql_error () );

  // Navigation Menu

  echo table_header ( $lang['Menu_User_Login'] );

  echo ' 
   <table cellpadding="5" cellspacing="0" border="0"> 
    <tr>
       ';

  // Add Listing
  echo '<td width="80" align="center" valign="top"><a href="' . URL . '/adduserlistings.php"><img src="' . URL . '/templates/' . $cookie_template . '/images/icons/addlistings.png" valign="top" border="0"><br />' . $lang['Add_Listings'] . '</a></td>';
  // Back to Control Panel
  echo '<td width="80" align="center" valign="top"><a href="' . URL . '/login.php"><img src="' . URL . '/templates/' . $cookie_template . '/images/icons/login.png" valign="top" border="0"><br />' . $lang['Menu_User_Login'] . '</a></td>';

  echo ' 
    </tr>
   </table>
       ';

  echo table_footer();

  //

  echo table_header ( $lang['Information'] );

  echo $lang['Order_By'] . ': 
  	<a href="' . URL . '/viewuserlistings.php?order_by=price&order_by2=asc">' . $lang['Listing_Price'] . ' ' . $lang['ASC'] . '</a>,
  	<a href="' . URL . '/viewuserlistings.php?order_by=price&order_by2=desc">' . $lang['Listing_Price'] . ' ' . $lang['DESC'] . '</a>,
  	<a href="' . URL . '/viewuserlistings.php?order_by=title&order_by2=asc">' . $lang['Listing_Title'] . ' ' . $lang['ASC'] . '</a>,
  	<a href="' . URL . '/viewuserlistings.php?order_by=title&order_by2=desc">' . $lang['Listing_Title'] . ' ' . $lang['DESC'] . '</a>,
  	<a href="' . URL . '/viewuserlistings.php?order_by=status&order_by2=asc">' . $lang['Listing_Status'] . ' ' . $lang['ASC'] . '</a>,
  	<a href="' . URL . '/viewuserlistings.php?order_by=status&order_by2=desc">' . $lang['Listing_Status'] . ' ' . $lang['DESC'] . '</a>,
  	<a href="' . URL . '/viewuserlistings.php?order_by=date_added&order_by2=asc">' . $lang['Listing_Added_Date'] . ' ' . $lang['ASC'] . '</a>,
  	<a href="' . URL . '/viewuserlistings.php?order_by=date_added&order_by2=desc">' . $lang['Listing_Added_Date'] . ' ' . $lang['DESC'] . '</a>,
  	<a href="' . URL . '/viewuserlistings.php?order_by=date_updated&order_by2=asc">' . $lang['Listing_Updated_Date'] . ' ' . $lang['ASC'] . '</a>,
  	<a href="' . URL . '/viewuserlistings.php?order_by=date_updated&order_by2=desc">' . $lang['Listing_Updated_Date'] . ' ' . $lang['DESC'] . '</a>
  	<br><br>  
   <table width="100%" cellpadding="5" cellspacing="0" border="0">
       ';

  $i = 0;

  $tpl = implode ('', file( PATH . '/templates/' . $cookie_template . '/tpl/property_search_short.tpl' ));

  while ($f = $db->fetcharray( $r2 ))
   {
    // Change all empty values in the array to 'n/a'
    $f = array_map ( 'if_empty', $f);

    // Starting a new template
    $template = new Template;

    // Load property short search results template
    $template->load ( $tpl );

    // VALUES

    // Replace the template variables
    
	// Default
	if ($f[0] == '')
		$f['title'] = $f['title'];
	else
		$f['title'] = $f[0];
		
	if ($f[1] == '')
		$f['description'] = $f['description'];
	else
		$f['description'] = $f[1];

    $template->set ( 'checkbox', '');
    $template->set ( 'favorites', '');

    $template->set ( 'link', URL . '/edituserlistings.php?id=' . $f['id']);
    $template->set ( 'image', show_image ('images', $f['id']) );
    $template->set ( 'mls', $f['mls'] );
    $template->set ( 'title', $f['title'] );
    $template->set ( 'type', getnamebyid ( TYPES_TABLE, $f['type'] ) );
    $template->set ( 'type2', getnamebyid ( TYPES2_TABLE, $f['type2'] ) );
    $template->set ( 'style', getnamebyid ( STYLES_TABLE, $f['style'] ) );

    $description = substr(removehtml(unsafehtml($f['description'])), 0, $conf['search_description']);
    $description = substr($description, 0, strrpos($description, ' ')) . ' ... ';
    $template->set ( 'description', $description );
    unset ($description);

    $template->set ( 'lot_size', $f['size'] );
    $template->set ( 'dimensions', $f['dimensions'] );

    if ($f['bathrooms'] < 1)
     $template->set ( 'bathrooms', '-' );
    else
     $template->set ( 'bathrooms', $f['bathrooms'] );

    $template->set ( 'half_bathrooms', $f['half_bathrooms'] );

    if ($f['bedrooms'] < 1)
     $template->set ( 'bedrooms', '-' );
    else
     $template->set ( 'bedrooms', $f['bedrooms'] );

    if ($f['garage_cars'] < 1)
     $template->set ( 'garage_cars', '-' );
    else
     $template->set ( 'garage_cars', $f['garage_cars'] );

    $cat = explode ("#", $f['location2']);
    $r11 = $db->query('SELECT category FROM ' . LOCATION1_TABLE . ' WHERE selector = "' . $cat[0] . '"');
    $f11 = $db->fetcharray($r11);
    $template->set ( 'location1', $f11['category'] );

    $r22 = $db->query('SELECT subcategory FROM ' . LOCATION2_TABLE . ' WHERE catsubsel = "' . $cat[1] . '"');
    $f22 = $db->fetcharray($r22);
    $template->set ( 'location2', $f22['subcategory'] );

    $r33 = $db->query('SELECT subsubcategory FROM ' . LOCATION3_TABLE . ' WHERE catsubsubsel = "' . $cat[2] . '"');
    $f33 = $db->fetcharray($r33);
    $template->set ( 'location3', $f33['subsubcategory'] );

    // If address is not allowed to be displayed
    if ($f['display_address'] == 'YES')
     {
      $template->set ( 'address1', $f['address1'] );
      $template->set ( 'address2', $f['address2'] );
      $template->set ( 'zip', $f['zip'] );
     }
    else
     {
      $template->set ( 'address1', ' ' );
      $template->set ( 'address2', ' ' );
      $template->set ( 'zip', ' ' );
     }

    $template->set ( 'price', pmr_number_format($f['price']) );
    $template->set ( 'currency', $conf['currency'] );
    $template->set ( 'directions', $f['directions'] );
    $template->set ( 'year_built', $f['year_built'] );
    $template->set ( 'buildings', show_multiple ( BUILDINGS_TABLE, $f['buildings'] ) );
    $template->set ( 'appliances', show_multiple ( APPLIANCES_TABLE, $f['appliances'] ) );
    $template->set ( 'features', show_multiple ( FEATURES_TABLE, $f['features'] ) );
    $template->set ( 'garage', getnamebyid ( GARAGE_TABLE, $f['garage'] ) );
    $template->set ( 'basement', getnamebyid ( BASEMENT_TABLE, $f['basement'] ) );
    $template->set ( 'date_added', printdate($f['date_added']) );
    $template->set ( 'date_updated', printdate($f['date_updated']) );
    $template->set ( 'date_upgraded', printdate($f['date_upgraded']) );
    $template->set ( 'ip_added', $f['ip_added'] );
    $template->set ( 'ip_updated', $f['ip_updated'] );
    $template->set ( 'ip_upgraded', $f['ip_upgraded'] );
    $template->set ( 'hits', $f['hits'] );
    $template->set ( 'new', newitem ( PROPERTIES_TABLE, $f['id'], $conf['new_days']) );
    $template->set ( 'updated', updateditem ( PROPERTIES_TABLE, $f['id'], $conf['updated_days']) );
    $template->set ( 'featured', featureditem ( $f['featured'] ) );

    // Realtor link
    $sql = 'SELECT * FROM ' . USERS_TABLE  . ' WHERE approved = 1 AND id = ' . $f['userid'] . ' LIMIT 1';
    $r_user = $db->query ( $sql ) or error ('Critical Error' , mysql_error());
    $f_user = $db->fetcharray ($r_user);

    $template->set ( 'view_realtor', '<a href="' . URL . '/viewuser.php?id=' . $f['userid'] . '">' . $f_user['first_name'] . ' ' . $f_user['last_name'] . '</a>');

    // Names

    $template->set ( '@mls', $lang['Listing_MLS'] );
    $template->set ( '@title', $lang['Listing_Title'] );
    $template->set ( '@type', $lang['Listing_Property_Type'] );
    $template->set ( '@type2', $lang['Module_Listing_Type'] );
    $template->set ( '@style', $lang['Listing_Style'] );
    $template->set ( '@description', $lang['Listing_Description'] );
    $template->set ( '@lot_size', $lang['Listing_Lot_Size'] );
    $template->set ( '@dimensions', $lang['Listing_Dimensions'] );
    $template->set ( '@bathrooms', $lang['Listing_Bathrooms'] );
    $template->set ( '@half_bathrooms', $lang['Listing_Half_Bathrooms'] );
    $template->set ( '@bedrooms', $lang['Listing_Bedrooms'] );
    $template->set ( '@location', $lang['Location'] );
    $template->set ( '@city', $lang['City'] );
    $template->set ( '@address1', $lang['Listing_Address1'] );
    $template->set ( '@address2', $lang['Listing_Address2'] );
    $template->set ( '@zip', $lang['Zip_Code'] );
    $template->set ( '@price', $lang['Listing_Price'] );
    $template->set ( '@directions', $lang['Listing_Directions'] );
    $template->set ( '@year_built', $lang['Listing_Year_Built'] );
    $template->set ( '@buildings', $lang['Listing_Additional_Out_Buildings'] );
    $template->set ( '@appliances', $lang['Listing_Appliances_Included'] );
    $template->set ( '@features', $lang['Listing_Features'] );
    $template->set ( '@garage', $lang['Listing_Garage'] );
    $template->set ( '@garage_cars', $lang['Listing_Garage_Cars'] );
    $template->set ( '@basement', $lang['Listing_Basement'] );
    $template->set ( '@date_added', $lang['Date_Added'] );
    $template->set ( '@date_updated', $lang['Date_Updated'] );
    $template->set ( '@date_upgraded', $lang['Date_Upgraded'] );
    $template->set ( '@hits', $lang['Hits'] );
    $template->set ( '@view_realtor', $lang['View_Realtor'] );
    $template->set ( '@image_url', URL . '/templates/' . $cookie_template . '/images' );

    //

    // Set background color
    $bgcolor = ''; // Background color for all odd listings
    $bgcolor2 = $conf['list_background_color_even']; // Background color for all even listings

    if ( $i%2 == 0 )
     $template->set ( 'bgcolor', $bgcolor );
    else
     $template->set ( 'bgcolor', $bgcolor2 );

    // Publish template
    $template->publish();

    $i++;

   }
 
  echo '
   </table>
       ';

  // Pagination

  echo '<br />';

  $results = $db->numrows( $r );

  if ( $results > $conf['search_results'] )

   {

    // Calculating the first and the last pages to show
    // - this makes the pages list smaller by showing the
    // relative results - 3 to the left and 3 to the right
    // from the current page
    if ((($page*$conf['search_results'])-($conf['search_results']*5)) >= 0) 
     $first=($page*$conf['search_results'])-($conf['search_results']*5);
    else 
     $first=0;

    if ((($page*$conf['search_results'])+($conf['search_results']*6)) <= $results) 
     $last =($page*$conf['search_results'])+($conf['search_results']*6);
    else 
     $last = $results;
    
    @    $i=$first/$conf['search_results'];

    // Previous Link
    if ($page > 0)
     {
      $pagenum = $page - 1;
      echo ' <a href="' . URL . '/viewuserlistings.php?page=' . $pagenum . '&order_by=' . $_GET['order_by'] . '&order_by2=' . $_GET['order_by2'] . '">' . $lang['Previous'] . '</a> | ';
     }

    // Pagination
    for ( $step = $first; $step < $last; $step=$step+$conf['search_results'] )

     {

      if ( $i == $page )

       {

 	$pagenum = $i+1;
	echo ' <span class="warning">' . $pagenum . '</span> | ';
	$i++;

       }

      else

       {

	$pagenum = $i+1;
	echo ' <a href="' . URL . '/viewuserlistings.php?page=' . $i . '&order_by=' . $_GET['order_by'] . '&order_by2=' . $_GET['order_by2'] . '">' . $pagenum . '</a> | ';
	$i++;

       }

     }

    // Next Link
    if ($page - (($results / $conf['search_results']) - 1) < 0)
     {
      $pagenum = $page+1;
      echo ' <a href="' . URL . '/viewuserlistings.php?page=' . $pagenum . '&order_by=' . $_GET['order_by'] . '&order_by2=' . $_GET['order_by2'] . '">' . $lang['Next'] . '</a>';
     }

   }

  // Pagination : END

  echo table_footer ();

 }

else

 error ('Critical Error' , 'Please login to access this script.');

// Template footer
include ( PATH . '/templates/' . $cookie_template . '/footer.php' );

?>